import React, { useState, useEffect } from 'react';
import {
  MenuFoldOutlined,
  MenuUnfoldOutlined,
  UploadOutlined,
  UserOutlined,
  VideoCameraOutlined,
} from '@ant-design/icons';
import { Button, Layout, Menu, theme,Space } from 'antd';
import Home from './child/Home';
import ShopList from './child/ShopList';
import TypeList from './child/TypeList';
import { Switch, Route, Redirect, type RouteComponentProps } from 'react-router-dom';

import * as async from '../store/crmsSlice';
import { useDispatch } from 'react-redux';
import { useAppDispatch } from '../store/reactreduxts';

const { Header, Sider, Content } = Layout;

//布局组件
const Admin: React.FC<RouteComponentProps> = (props) => {

  //获取数据
  let dispatch = useAppDispatch()
  //初始周期中调取异步方法 获取商品异步
  useEffect(() => {
    dispatch(async.async_shoplist())
    dispatch(async.async_typelist())
  }, [])


  const [collapsed, setCollapsed] = useState(false);
  const {
    token: { colorBgContainer, borderRadiusLG },
  } = theme.useToken();

  return (
    <Layout style={{ height: '100vh' }}>
      <Sider trigger={null} collapsible collapsed={collapsed}>
        <div className="demo-logo-vertical" />
        <Menu
          theme="dark"
          mode="inline"
          defaultSelectedKeys={[props.location.pathname]}
          defaultOpenKeys={['', props.location.pathname]}
          items={[
            {
              key: '/admin/home',
              icon: <UserOutlined />,
              label: '首页',
            },
            {
              key: '',
              icon: <VideoCameraOutlined />,
              label: '商品管理',
              children: [
                {
                  key: '/admin/shoplist',
                  icon: <UploadOutlined />,
                  label: '商品列表',
                },
                {
                  key: '/admin/typelist',
                  icon: <UploadOutlined />,
                  label: '分类列表',
                },
              ]
            },
          ]}
          onSelect={(e) => {
            //编程式跳转
            props.history.push(e.key)
          }}
        />
      </Sider>
      <Layout>
        <Header style={{ padding: 0, background: colorBgContainer }}>
          <Button
            type="text"
            icon={collapsed ? <MenuUnfoldOutlined /> : <MenuFoldOutlined />}
            onClick={() => setCollapsed(!collapsed)}
            style={{
              fontSize: '16px',
              width: 64,
              height: 64,
            }}
          />
          <Space style={{position:'absolute',right:'20px'}}>
            <span>用户名:{localStorage.getItem('token')}</span>
            <span onClick={()=>{
              //1.清除本地缓存
              localStorage.removeItem('token')
              //2.跳转到登录页
              props.history.push('/login')
            }}>退出登录</span>
          </Space>
        </Header>
        <Content
          style={{
            margin: '24px 16px',
            padding: 24,
            minHeight: 280,
            background: colorBgContainer,
            borderRadius: borderRadiusLG,
          }}
        >
          {/* 配置二级路由 */}
          <Switch>
            <Route path='/admin/home' component={Home} />
            <Route path='/admin/shoplist' component={ShopList} />
            <Route path='/admin/typelist' component={TypeList} />
            <Redirect from='/admin' to='/admin/home' />
          </Switch>
        </Content>
      </Layout>
    </Layout>
  );
};

export default Admin;
